import os
from flask import Flask,request,render_template
from flask_uploads import UploadSet,IMAGES,configure_uploads,patch_request_class
app = Flask(__name__)
#配置上传大小
app.config['MAX_CONTENT_LENGTH'] = 1024*1024*8

#配置上传的位置
app.config['UPLOADED_PHOTOS_DEST'] = os.getcwd()


#创建上传对象
photos = UploadSet('photos',IMAGES)
configure_uploads(app,photos)
patch_request_class(app,size=None) #默认64M 如果size为None 那么就按照MAX_CONTENT_LENGTH 配置的大小 8m




@app.route('/')
def hello_world():
    return 'Hello World，flask-uploads!'

@app.route('/upload/',methods=['GET','POST'])
def upload():
    img_url = None
    if request.method == 'POST' and 'photo' in request.files:
        #保存文件
        filename = photos.save(request.files['photo'])
        #save()保存后的文件名字
        #获取图片的url
        img_url = photos.url(filename)
    return render_template('index.html',img_url=img_url)


if __name__ == '__main__':
    app.run()
